<?php
/**
 * 附件管理 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: mod_attach.php 523 2010-06-23 10:06:24Z lisijie86 $
*/
!defined('IN_ADMIN') && exit('Access Denied!');

check_perm_prompt('attach','manage');

empty($ac) && $ac = 'list';

if ($ac == 'list') {

	initGP(array('page','year','month','day'),'G','int');
	$page = max(1, $page);
	$pagesize = 20;
	$offset = ($page - 1) * $pagesize;
	$wheresql = $orderby = '';
	$url = '?mod=attach';

	if ($year > 0) {
		$stime = mktime(0,0,0,max(1,$month),max(1,$day),$year);
		if ( $month ) {
			$etime = strtotime('+1 month', $stime);
			if ( $day ) {
				$etime = strtotime('+1 day', $stime);
			}
		} else {
			$etime = strtotime('+1 year', $stime);
		}
		$wheresql .= " AND dateline BETWEEN $stime AND $etime";
		$url .= "&year={$year}&month={$month}&day={$day}";
	}
	if ( !empty($_GET['fileext']) ) {
		$wheresql .= " AND fileext = '".check_str($_GET['fileext'])."'";
		$url .= "&fileext=".$_GET['fileext'];
	}
	if ( $_GET['keyword'] ) {
		$keyword = check_str($_GET['keyword']);
		$wheresql .= " AND originalname LIKE '%$keyword%'";
		$url .= "&keyword=".rawurlencode($keyword);
	}
	if ( $_GET['id'] == 'asc') {
		$orderby .= empty($orderby) ? 'id asc' : ',id asc'; 
		$url .= '&id=asc';
	}
	if ( $_GET['filesize'] ) {
		$o = $_GET['filesize'] == 'asc' ? 'asc' : 'desc'; 
		$orderby .= empty($orderby) ? 'filesize '.$o : ',filesize '.$o; 
		$url .= '&filesize='.$o;
	}
	if ( $_GET['downloads'] ) {
		$o = $_GET['downloads'] == 'asc' ? 'asc' : 'desc'; 
		$orderby .= empty($orderby) ? 'downloads '.$o : ',downloads '.$o; 
		$url .= '&downloads='.$o;
	}

	empty($orderby) && $orderby = 'id DESC';

	$count = $db->count('attach', "1 $wheresql");
	$result = array();
	if ( $count ) {
		$sql = "SELECT * FROM ".tname('attach')." WHERE 1 $wheresql ORDER BY $orderby LIMIT $offset, $pagesize";
		$result = $db->fetch_all($sql);
	}

	include admin_tpl('attach');

} elseif ($ac == 'delete') {
	
	$ids = array();
	foreach ( (array)$_POST['id'] as $id ) {
		if ( is_numeric($id) ) {
			$ids[] = (int)$id;
		}
	}
	foreach ($ids as $id) {
		$file = $db->fetch_one_array("SELECT filepath FROM ".tname('attach')." WHERE id = '$id'");
		@unlink(JBLOG_ROOT.$file['filepath']);
	}
	$db->delete('attach', array('id'=>$ids));
	redirect($_USER['refer']);

}

function get_fileext_options($selext = '') {
	global $db;
	$output = '';
	$sql = "SELECT DISTINCT fileext FROM ".tname('attach')."";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$selstr = $selext == $row['fileext'] ? 'selected="selected"' : '';
		$output .= '<option vlaue="'.$row['fileext'].'" '.$selstr.'>'.$row['fileext'].'</option>';
	}
	return $output;
}

function get_date_options($year=0, $month=0, $day=0) {
	global $db;
	$output = '';
	$sql = "SELECT MIN(dateline) AS mindate,MAX(dateline) AS maxdate FROM ".tname('attach')."";
	$result = $db->fetch_one_array($sql);
	$minyear = get_date($result['mindate'],'Y');
	$minmonth = get_date($result['mindate'],'n');
	$maxyear = get_date($result['maxdate'],'Y');
	$maxmonth = get_date($result['maxdate'],'n');
	$output .= '<select name="year"><option value=""></option>';
	for($i=$minyear; $i<=$maxyear; $i++) {
		$selstr = $year == $i ? 'selected="selected"' : '';
		$output .= '<option value="'.$i.'" '.$selstr.'>'.$i.'</option>';
	}
	$output .= '</select>'.__('年');
	if ($minyear != $maxyear) {
		$minmonth = 1; $maxmonth = 12;
	}
	$output .= '<select name="month"><option value=""></option>';
	for($i=$minmonth; $i<=$maxmonth; $i++) {
		$selstr = $month == $i ? 'selected="selected"' : '';
		$output .= '<option value="'.$i.'" '.$selstr.'>'.$i.'</option>';
	}
	$output .= '</select>'.__('月');
	$output .= '<select name="day"><option value=""></option>';
	for($i=1; $i<=31; $i++) {
		$selstr = $day == $i ? 'selected="selected"' : '';
		$output .= '<option value="'.$i.'" '.$selstr.'>'.$i.'</option>';
	}
	$output .= '</select>'.__('日');
	return $output;
}
?>